﻿using System;
using System.ComponentModel;
using System.Threading;
using Airborne.ScrumPoker.Module.Properties;

namespace Airborne.ScrumPoker.Module
{
    public class CountdownTimer : INotifyPropertyChanged, IDataErrorInfo
    {
        #region Fields
        private int currentCount;
        private bool timeStarted;
        #endregion

        #region TimerComplete

        public event EventHandler<EventArgs> TimerComplete;

        private void RaiseTimerComplete()
        {
            EventArgs args = new EventArgs();
            OnTimerComplete(args);
        }

        protected virtual void OnTimerComplete(EventArgs args)
        {
            EventHandler<EventArgs> handler = this.TimerComplete;
            if (handler != null)
            {
                handler(this, args);
            }
        }

        #endregion

        #region Contructors
        public CountdownTimer()
        {
            Timer = new System.Threading.Timer(TimerCallback, this, Timeout.Infinite, 1000);
        }
        #endregion

        #region Properties
        private Timer Timer { get; set; }

        public int CurrentCount
        {
            get
            {
                return currentCount;
            }
            set
            {
                if (currentCount != value)
                {
                    currentCount = value;
                    RaisePropertyChanged("CurrentCount");
                }
            }
        }

        public bool TimerStarted
        {
            get
            {
                return timeStarted;
            }
            set
            {
                if (timeStarted != value)
                {
                    timeStarted = value;
                    if (value)
                    {
                        Timer.Change(0, 1000);
                    }
                    else
                    {
                        Timer.Change(Timeout.Infinite, 1000);
                    }
                    RaisePropertyChanged("TimerStarted");
                }
            }
        }
        #endregion

        #region Methods
        private void TimerCallback(object state)
        {
            if (CurrentCount > 0)
            {
                CurrentCount = CurrentCount - 1;
            }
            else
            {
                RaiseTimerComplete();
            }
        }

        public void Start(int seconds)
        {
            CurrentCount = seconds;
            TimerStarted = true;
        }

        public void Stop()
        {
            TimerStarted = false;
        }


        #endregion



        #region PropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventArgs args = new PropertyChangedEventArgs(propertyName);
            OnPropertyChanged(args);
        }

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, args);
            }
        }

        #endregion

        #region IDataErrorInfo Members

        public string Error
        {
            get 
            { 
                return string.Empty; 
            }
        }

        public string this[string columnName]
        {
            get 
            {
                if (columnName == "CurrentCount")
                {
                    if (CurrentCount <= 0)
                    {
                        return Resources.ErrorValueMustBeGreaterThan0;
                    }
                }
                return string.Empty;
            }
        }

        #endregion
    }
}
